package com.practice.sort;

import java.util.ArrayList;
import java.util.Scanner;

public class Sorting {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Integer> list1 = new ArrayList<Integer>();
        System.out.println("请输入本次排序数组的长度:");
        int n = scanner.nextInt();
        System.out.println("OK!本次排序的数组长度为:"+n);
        for (int i = 0; i < n; i++) {
            System.out.println("===============================");
            System.out.println("请输入第"+(i+1)+"个数:");
            int x = scanner.nextInt();
            System.out.println(x);
            list1.add(x);
        }
        System.out.println("==================================");
        System.out.println("键盘输入的数组为:"+list1);
        sorting(list1);
    }

    private static void sorting(ArrayList<Integer> list1) {
        int length = list1.toArray().length;
        int tmp;
        try {
            for (int j = 0; j < length - 1; j++) {
                for (int i = 0; i < length - 1 - j; i++) {
                    int a = list1.get(i);
                    int b = list1.get(i + 1);
                    if (a > b) {
                        tmp = b;
                        b = a;
                        a = tmp;
                        list1.set(i,a);
                        list1.set(i+1,b);
                    }
                }
            }
            System.out.println("排序过后的数组为:"+list1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
